﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RemoveLinkedListElements
{

    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int x) { val = x; }
    }

    public class Solution
    {
        public ListNode RemoveElements(ListNode head, int val)
        {
            ListNode p = new ListNode(0)
            {
                next = head
            };

            ListNode q = head;
            head = p;

            while (q != null)
            {
                if (q.val == val)
                {
                    p.next = q.next;
                    p = p.next;
                    q = q.next;
                }
            }
            return head.next;
            
        }
    }
}
